package com.wl.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.wl.entity.Apply_01;
import com.wl.entity.Approve_02;
import com.wl.entity.Approve_03;
import com.wl.entity.Buyerapprove_04;
import com.wl.entity.Loginfo;
import com.wl.entity.Warehouse_06;
import com.wl.service.Apply_01Service;
import com.wl.service.Approve_02Service;
import com.wl.service.Approve_03Service;
import com.wl.service.BuyerApprove_04Service;
import com.wl.service.Warehouse_06Service;
import com.wl.util.CommonMessage;
import com.wl.util.PageData;

@Controller
public class Approve03Controller {

	// 注入03环节service
	@Autowired
	private Approve_03Service approve03s;

	// 注入04环节service
	@Autowired
	private BuyerApprove_04Service buyer04s;

	// 注入06环节service
	@Autowired
	private Warehouse_06Service ware06s;

	// 注入01环节service
	@Autowired
	private Apply_01Service apply01s;

	/**
	 * 查询未处理流程并响应到页面 分页
	 * 
	 * @param page      当前页
	 * @param limit     每页记录数
	 * @param managerno 03环节处理人
	 * @return
	 */
	@GetMapping(value = "nothandby03", produces = "application/json;charset=utf-8")
	@ResponseBody
	public PageData<Approve_03> getApprove02Handler(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "10") int limit,
			@RequestParam(value = "managerno", defaultValue = "") String managerno) {
		// 调用service中的显示未处理流程的方法
		PageData<Approve_03> pd = approve03s.selectNotHandByName(managerno, page, limit);
		System.out.println(pd);
		return pd;
	}

	/**
	 * 03提交04时 给03表里增加数据 给04表里增加待办数据
	 * 
	 * @param approve02
	 * @return
	 */
	@PostMapping(value = "addData03to04", produces = "application/json;charset=utf-8", consumes = "application/json")
	@ResponseBody
	public boolean addApprove03Data(@RequestBody Approve_03 approve03) {

		// 获取前台传过来的参数
		String flowid = approve03.getFlowid();
		int approvestatus = approve03.getApprovestatus();// 是否同意
		String managerdetail = approve03.getManagerdetail();
		String nexthandler = approve03.getNexthandler();
		String applyno = approve03.getApplyno();
		String managerno = approve03.getManagerno();// 当前处理人

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String submitdate = sdf.format(new Date());

		// 创建03环节对象 用于03环节提交 增加03的审批意见 未处理变已处理
		Approve_03 app03 = new Approve_03(null, flowid, managerno, managerdetail, submitdate, approvestatus, 1, applyno,
				nexthandler);

		// 创建Loginfo对象 用于增加日志
		Loginfo log = new Loginfo();
		log.setPerson(managerno);
		log.setFlowid(flowid);
		log.setLogcontent("03环节提交到04采购环节");
		log.setLogdate(submitdate);

		// 调用03环节service中的修改语句
		boolean f = approve03s.updateByflowid(app03, log);
		System.out.println("03表里数据是否更新成功：" + f);

		// 创建04环节对象 用于给04表里增加待办信息
		Buyerapprove_04 buyer04 = new Buyerapprove_04(flowid, nexthandler, null, null, 0, applyno);

		// 调用04环节service中的增加方法
		int n = buyer04s.insert(buyer04);
		return n > 0;
	}

	/**
	 * 03提交06时 给03表里增加数据 给06表里增加待办数据
	 * 
	 * @param approve02
	 * @return
	 */
	@PostMapping(value = "addData03to06", produces = "application/json;charset=utf-8", consumes = "application/json")
	@ResponseBody
	public boolean addApprove03Datato06(@RequestBody Approve_03 approve03) {

		// 获取前台传过来的参数
		String flowid = approve03.getFlowid();
		int approvestatus = approve03.getApprovestatus();// 是否同意
		String managerdetail = approve03.getManagerdetail();
		String nexthandler = approve03.getNexthandler();
		String applyno = approve03.getApplyno();
		String managerno = approve03.getManagerno();// 当前处理人

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String submitdate = sdf.format(new Date());

		// 创建03环节对象 用于03环节提交 增加03的审批意见 未处理变已处理
		Approve_03 app03 = new Approve_03(null, flowid, managerno, managerdetail, submitdate, approvestatus, 1, applyno,
				nexthandler);

		// 创建Loginfo对象 用于增加日志
		Loginfo log = new Loginfo();
		log.setPerson(managerno);
		log.setFlowid(flowid);
		log.setLogcontent("03环节提交到06库房环节");
		log.setLogdate(submitdate);

		// 调用03环节service中的修改语句
		boolean f = approve03s.updateByflowid(app03, log);
		System.out.println("03表里数据是否更新成功：" + f);

		// 创建06环节对象 用于给06表里增加待办信息
		Warehouse_06 ware06 = new Warehouse_06();
		ware06.setFlowid(flowid);
		ware06.setApplyno(applyno);
		ware06.setAppstatus(1);
		ware06.setAppno(nexthandler);

		// 调用06环节service中的增加方法
		return ware06s.insertSelective(ware06);
	}

	/**
	 * 03退回给01 更新03表的状态为已处理 更新01表的状态为待处理
	 * 
	 * @param approve02
	 * @return
	 */
	@PostMapping(value = "addData03to01", produces = "application/json;charset=utf-8", consumes = "application/json")
	@ResponseBody
	public boolean Approve03Datato01(@RequestBody Approve_03 approve03) {

		// 获取前台传过来的参数
		String flowid = approve03.getFlowid();
		int approvestatus = approve03.getApprovestatus();// 是否同意
		String managerdetail = approve03.getManagerdetail();
		String nexthandler = approve03.getNexthandler();
		String applyno = approve03.getApplyno();
		String managerno = approve03.getManagerno();// 当前处理人

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String submitdate = sdf.format(new Date());

		// 创建03环节对象 用于03环节提交 增加03的审批意见 未处理变已处理
		Approve_03 app03 = new Approve_03(null, flowid, managerno, managerdetail, submitdate, approvestatus, 1, applyno,
				null);

		// 创建Loginfo对象 用于增加日志
		Loginfo log = new Loginfo();
		log.setPerson(managerno);
		log.setFlowid(flowid);
		log.setLogcontent("03环节退回给01环节");
		log.setLogdate(submitdate);

		// 调用03环节service中的修改语句 更新为退回状态
		boolean f = approve03s.updateByflowid(app03, log);
		System.out.println("03表里数据是否更新成功：" + f);

		// 创建06环节对象 用于给06表里增加待办信息
		Apply_01 apply01 = new Apply_01();
		apply01.setFlowid(flowid);
		apply01.setApplystatus(0);

		// 调用01环节service中的修改为待办任务的方法
		return apply01s.updateBackByFlowid(apply01);
	}
	
	
	/**
	 * 查询已处理流程并响应到页面 分页
	 * 
	 * @param page      当前页
	 * @param limit     每页记录数
	 * @param managerno 03环节处理人
	 * @return
	 */
	@GetMapping(value = "myapproved03", produces = "application/json;charset=utf-8")
	@ResponseBody
	public PageData<Approve_03> getApprovedHandler03(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "10") int limit,
			@RequestParam(value = "managerno", defaultValue = "") String managerno) {
		// 调用service中的显示未处理流程的方法
		PageData<Approve_03> pd = approve03s.selectApprovedByName(managerno, page, limit);
		System.out.println(pd);
		return pd;
	}

}
